/*
 * @Author       : 邱狮杰
 * @Date         : 2021-05-19 10:21:33
 * @LastEditTime : 2021-05-19 10:30:49
 * @FilePath     : /wedding-dress-selection/src/reducer/mainTitle.tsx
 * @Description  : title
 */

import { createContext, useReducer } from 'react'
import { Dispatch } from 'react-dom/node_modules/@types/react'
export const CreateTitle = createContext({})

interface stateConfig {
  name: string
}
const initTitle: stateConfig = { name: '' }
const GET = 'get'
const RESET = 'reset'
interface titleAction {
  type: typeof GET | typeof RESET
  data: stateConfig
}
export interface useTitleContextType {
  titleState: stateConfig
  titleDispatch: Dispatch<titleAction>
}
export function titleReducer(state: stateConfig, action: titleAction) {
  switch (action.type) {
    case GET:
      return state
    case RESET:
      return action.data
    default:
      return state
  }
}

export const ReturnTitleContext = (props: any) => {
  const [titleState, titleDispatch] = useReducer(titleReducer, initTitle)
  return <CreateTitle.Provider value={{ titleState, titleDispatch }}>{props.children}</CreateTitle.Provider>
}
